package february.test_2_26;

import java.util.*;

public class TestMain2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int min = 0;
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        List<Integer> list = foundApproximate(n);
        for (int i = 0; i < list.size(); i++) {
            Deque<Integer> stack = new LinkedList<>();
            if(isResult(n+list.get(i),m,stack)){
                int count = stack.size() + 1;
                min = Math.min(count,min);
            }
        }
        System.out.println(min);
    }
    public static Boolean isResult(int n, int m, Deque<Integer> stack) {
        List<Integer> list = foundApproximate(n);
        if(n == m) {
            return true;
        }
        if(n > m || list.size() == 0) {
            return false;
        }
        stack.push(n);
        for (int i = 0; i < list.size(); i++) {
            boolean boo = isResult(n + list.get(i),m,stack);
            if(boo) {
                return true;
            } else {
                stack.pop();
                return false;
            }
        }
        return false;
    }
    public static List<Integer> foundApproximate(int n) {
        List<Integer> list = new ArrayList<>();
        for(int i = 2; i < n; i++) {
            if(n % i == 0) {
                list.add(i);
            }
        }
        return list;
    }
}
